package com.liaoye.helloprotocolbuffer;

import androidx.appcompat.app.AppCompatActivity;

import android.annotation.SuppressLint;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.Toast;

public class MainActivity extends AppCompatActivity implements View.OnClickListener {
    AddressBookProto.Person mPerson = null;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        Button btnSet = findViewById(R.id.btn_set);
        Button btnGet = findViewById(R.id.btn_get);
        btnSet.setOnClickListener(this);
        btnGet.setOnClickListener(this);
    }

    @SuppressLint("NonConstantResourceId")
    @Override
    public void onClick(View v) {
        int viewId = v.getId();
        switch (viewId) {
            case R.id.btn_set:
                mPerson = AddressBookProto.Person.newBuilder()
                                .setId(1234)
                                .setName("John Doe")
                                .setEmail("jdoe@example.com")
                                .addPhones(
                                        AddressBookProto.Person.PhoneNumber.newBuilder()
                                                .setNumber("555-4321")
                                                .setType(AddressBookProto.Person.PhoneType.HOME))
                                .build();
                Toast.makeText(MainActivity.this,"设置数据成功", Toast.LENGTH_LONG).show();
                break;
            case R.id.btn_get:
                if(mPerson == null){
                    Toast.makeText(MainActivity.this,"请先点击设置按钮", Toast.LENGTH_LONG).show();
                    return;
                }
                Toast.makeText(MainActivity.this, mPerson.toString(), Toast.LENGTH_LONG).show();
                break;
            default:
                break;
        }
    }
}